#include "BaseTime.h"
#include <time.h>
#ifdef _WIN32
#include <windows.h>
#endif // _WIN32

namespace BaseLib
{
#ifdef  _WIN32
    class Win32PerformanceCounter
    {
    public:
        Win32PerformanceCounter()
        {
            // get the "ticks per second" value
            QueryPerformanceFrequency((LARGE_INTEGER*)(&m_frequency));

            // convert it into a "ticks per millisecond" value
            m_frequency = m_frequency / 1000;
        }

        // this is the frequency of the performance counter, in 
        // "ticks per millisecond"
        sint64 m_frequency;
    };

    Win32PerformanceCounter g_win32counter;
#endif //  _WIN32
    
    sint64 GetTimeMS()
    {
        sint64 t;
        QueryPerformanceCounter((LARGE_INTEGER*)(&t));
        return t / g_win32counter.m_frequency;
    }

    sint64 GetTimeS()
    {
        return GetTimeMS() / 1000;
    }

    sint64 GetTimeM()
    {
        return GetTimeMS() / 60000;
    }

    sint64 GetTimeH()
    {
        return GetTimeMS() / 3600000;
    }

    std::string TimeStamp()
    {
        char str[9];

        time_t a = time(0);
        struct tm* b = localtime(&a);
        strftime(str, 9, "%H:%M:%S", b);

        return str;
    }

    std::string DataStamp()
    {
        char str[11];

        time_t a = time(0);
        struct tm* b = localtime(&a);

        strftime(str, 11, "%Y.%m.%d", b);

        return str;
    }



    Timer::Timer()
    {
        m_starttime = 0;
        m_inittime = 0;
    }

    void Timer::Reset(sint64 p_timepassed)
    {
        m_starttime = p_timepassed;
        m_inittime = GetTimeMS();
    }

    sint64 Timer::GetMS()
    {
        return (GetTimeMS() - m_inittime) + m_starttime;
    }
    sint64 Timer::GetS()
    {
        return GetMS() / 1000;
    }

    sint64 Timer::GetM()
    {
        return GetMS() / 60000;
    }

    sint64 Timer::GetH()
    {
        return GetMS() / 3600000;
    }

    sint64 Timer::GetD()
    {
        return GetMS() / 86400000;
    }

    sint64 Timer::GetY()
    {
        return GetD() / 365;
    }

    std::string Timer::GetString()
    {
        std::string str;

        return str;
    }
}
